package com.qf.sl.youshare.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.qf.sl.youshare.Constants;
import com.qf.sl.youshare.R;
import com.qf.sl.youshare.model.Circle;
import com.qf.sl.youshare.utils.FileCache;
import com.qf.sl.youshare.utils.ImageCache;
import com.qf.sl.youshare.utils.ImageLoader;

import java.util.LinkedList;

/**
 * Created by IDEA
 * User : SL
 * on  2015/2/22.
 * 20:36
 */
public class SheJiaoCircleAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private LinkedList<Circle> circles;

    public SheJiaoCircleAdapter(Context context, LinkedList<Circle> circles) {

        if (context != null) {
            inflater = LayoutInflater.from(context);
        }else {
            throw new IllegalArgumentException("Context must not null");
        }
        this.circles = circles;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (circles != null) {
            ret = circles.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (circles != null) {
            ret = circles.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;

        if (convertView != null) {
            ret = convertView;
        }else {
            ret = inflater.inflate(R.layout.item_shejiao_circle, parent, false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder == null) {
            holder = new ViewHolder();

            holder.itemImageHolder = (ImageView) ret.findViewById(R.id.shejiao_circle_item_image_bg);
            holder.itemHolder = (TextView) ret.findViewById(R.id.shejiao_circle_item_title);
            holder.tagHolder = (TextView) ret.findViewById(R.id.shejiao_circle_tag);
            holder.peopleCountHolder = (TextView) ret.findViewById(R.id.shejiao_circle_people_count);

            ret.setTag(holder);
        }

        Circle circle = circles.get(position);

        String groupName = circle.getGroupName();
        String tag = circle.getTag();
        String countUser = circle.getCountUser();
        String photo = circle.getPhoto();

        holder.itemHolder.setText(groupName);
        holder.tagHolder.setText(tag);
        holder.peopleCountHolder.setText(countUser);

        if (photo != null && photo.length() > 0) {
            if (!photo.startsWith("http://") && !photo.startsWith("https://")){
                photo = Constants.IMAGE_SERVER + photo;
            }

            holder.itemImageHolder.setTag(photo);

            holder.itemImageHolder.setImageResource(R.drawable.product_default);

            ImageCache imageCache = ImageCache.getInstance();
            Bitmap bitmap = imageCache.getImage(photo);
            if (bitmap != null) {
                holder.itemImageHolder.setImageBitmap(bitmap);
            }else {
                FileCache fileCache = FileCache.getInstance();
                byte[] bytes = fileCache.getFileContent(photo);
                if (bytes != null && bytes.length > 0) {
                    Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    holder.itemImageHolder.setImageBitmap(bmp);

                    imageCache.putImage(photo, bmp);
                }else {
                    ImageLoader task = new ImageLoader(holder.itemImageHolder);
                    task.execute(photo);
                }
            }

        }

        return ret;
    }

    private class ViewHolder{
        public ImageView itemImageHolder;
        public TextView itemHolder;
        public TextView tagHolder;
        public TextView peopleCountHolder;
    }
}
